<?php
class CommodityAdminAction extends Action{
	public function index(){
		$this->validate();
		$seriesStr = new series();
		$storeModel = D("store");
		$seriesModel = D("series");
		$merchandiseModel = D("merchandise");
		$vo = $storeModel->join("suser on suser.suser_id = store.store_mid")
		->where("suser.suser_id='".$_SESSION['suser']['account']."'")->find();
		
		$series = $seriesModel
		->where("series.series_sid='".$vo['store_id']."'")->find();
		$commoditycount = 0;
		foreach ($seriesStr->seriesStr as $key => $value){
			${'series_'.$value} =  $merchandiseModel
				->where("mcd_sid='".$vo['store_id']."' and mcd_type='".$value."' and mcd_mid=''")->findAll();
			${'linecount_'.$value} = floor(count(${'series_'.$value})/5);
			${'surpluscount_'.$value} = count(${'series_'.$value})%5;
			$commoditycount += count(${'series_'.$value});
		}
		
		$seriescountArray = $series;
		$seriescount = count(array_filter($seriescountArray));
		//dump($vo);
		//dump($series);
		
		$this->assign("vo", $vo);
		$this->assign("series",$series);
		$this->assign('commoditycount',$commoditycount);
		foreach ($seriesStr->seriesStr as $key => $value){
			$this->assign("series_".$value,${'series_'.$value});
			$this->assign("linecount_".$value,${'linecount_'.$value});
			$this->assign("surpluscount_".$value,${'surpluscount_'.$value});
			$this->assign("commoditycount_".$value,count(${'series_'.$value}));
		}
		$this->assign("seriescount",$seriescount-1);
		$this->display();
	}
	
	public function filter($var)
	{
        if($var == '')
        {
                return false;
        }

        return true;
	}

	//登陆信息验证
	public function validate(){
		if(empty($_SESSION['suser'])){
			$this->redirect('Index/index');
		}
	}
	
	//更新商品系列
	public function updateSeries(){
		$this->validate();
		if(!empty($_POST['value']) && !empty($_POST['series'])) {
			$seriesModel = D("series");
			$storeModel = D("store");
			$result = $storeModel->field('store_id')->where("store_mid='".$_SESSION['suser']['account']."'")->find();
			if($result!==false){
				$data[$_POST['series']] = $_POST['value'];
				$addResult = $seriesModel->where("series_sid = '".$result['store_id']."'")->save($data);
				$vo['value'] = $_POST['value'];
				$vo['series'] = $_POST['series'];
				if($addResult!==false){
					$this->ajaxReturn($vo,'更新商品系列成功！',1);
				}ELSE{
					$this->ajaxReturn($vo,'更新商品系列失败，请重试！',0);
				}
			}else{
				$this->ajaxReturn($vo,'更新商品系列失败，请重试！',0);
			}
		}
	}
	//添加商品
	public function  addCommodity(){
		$this->validate();
		$count = trim($_POST['count']);
		$series = trim($_POST['series']);
		$level = trim($_POST['level']);
		if(!empty($series)){
			if(!empty($count) && $count>0){
				$merchindexModel = D("merchindex");
				$merchandiseModel = D("merchandise");
				$result = $merchindexModel->field('merch_maxindex')
						->where("merch_sid='".$_SESSION['suser']['store_id']."' and merch_seid='".$series."' and merch_level='".$level."'")
						->find();
						$merchindexModel->getLastSql();
				if($result){
					$maxindex = (int)$result['merch_maxindex'];
				}else{
					$maxindex = 0;
				}
				$strf  = substr($_SESSION['suser']['store_id'],0,1);
				$strf .= (string)substr($_SESSION['suser']['store_id'],-5);
				$strf .= '-';
				$strm  = '-';
				$strm .= $level;
				$strm .= '-';
				$strm .= $series;
				$index = 0;
				$resultMcd =  array();
				$merchandiseModel->startTrans();
				for($i=0;$i<$count;$i++){
					$data = array();
					$data['mcd_id'] = $strf.strrev(date("ymdHis")).$strm.str_pad($maxindex+$index+1,4,'0',STR_PAD_LEFT);
					$resultMcd['mcd_id'][] = $data['mcd_id'];
					$data['mcd_sid'] = $_SESSION['suser']['store_id'];
					$data['mcd_type'] = $series;
					$data['mcd_update'] = date("Y-m-d H:i:s");
					if(false!==$merchandiseModel->add($data)){
						$index += 1; 
					}
					
				}
				
				$result = $merchindexModel->field('merch_maxindex')
						->where("merch_sid='".$_SESSION['suser']['store_id']."' and merch_seid='".$series."' and merch_level='".$level."'")
						->find();
				
				if($result){
					$maxindexNew = (int)$result['merch_maxindex'];
				}else{
					$maxindexNew = 0;
				}		
				if($maxindex==$maxindexNew){
					$successNum = $index;
					if($maxindex!=0){
						$data = array();
						$data['merch_maxindex'] = $maxindexNew+$successNum;
						$updateResult = $merchindexModel
						->where("merch_sid='".$_SESSION['suser']['store_id']."' and merch_seid='".$series."' and merch_level='".$level."'")
						->save($data);
						if($updateResult!==false){
							$merchandiseModel->commit();
							$resultMcd['count'] = $successNum;
							$this->ajaxReturn($resultMcd,'成功创建'.$successNum.'个货架！',1);
						}else{
							$merchandiseModel->rollback();
							$this->ajaxReturn($successNum,'添加货架失败1！请重试！',0);
						}
					}else{
						$data = array();
						$data['merch_sid'] = $_SESSION['suser']['store_id'];
						$data['merch_seid'] = $series;
						$data['merch_level'] = $level;
						$data['merch_maxindex'] = $maxindexNew+$successNum;
						if(false!==$merchindexModel->add($data)){
							$merchandiseModel->commit();
							$resultMcd['count'] = $successNum;
							$this->ajaxReturn($resultMcd,'成功创建'.$successNum.'个货架！',1);
						}else{
							$merchandiseModel->rollback();
							$this->ajaxReturn($successNum,'添加货架失败2！请重试！',0);
						}
					}
					
				}else{
					$merchandiseModel->rollback();
					$this->ajaxReturn('','数据不同步，请尝试重新创建！',0);
					
				}
			}else{
			$vo = "";
			$this->ajaxReturn($vo,'货架个数错误！请重试！',0);
			}
		}else{
			$vo = "";
			$this->ajaxReturn($_POST,'添加货架失败3！请重试！',0);
		}
	}  
	
	//删除商品
	public function deleteCommodity(){
		$this->validate();
		$_POST['id'] = trim($_POST['id']);
		if(!empty($_POST['id'])){
			$merchandiseModel = D("merchandise");
			$result	=	$merchandiseModel->where("mcd_id='".$_POST['id']."'")->delete();
			if(false !== $result) {
				$this->ajaxReturn($_POST['id'],'删除成功！',1);
			}else{
				$this->error('删除出错！');
			}
		}else{
			$vo = "";
			$this->ajaxReturn($vo,'删除商品系列失败，请重试！',0);
		}
		
	}
	
	//隐藏系列
	public function showOrHideSeries(){
		$this->validate();
		if(!empty($_POST['shelves']) && ($_POST['type']!='') && !empty($_POST['series_sid'])){
			$seriesModel = D('series');
			$data['series_'.trim($_POST['shelves']).'_show'] = trim($_POST['type']);
			if(false !==$seriesModel->where("series_sid='".trim($_POST['series_sid'])."'")->save($data)){
				$this->ajaxReturn($_POST['type'],'设置商品系列成功！',1);
			}else{
				$this->ajaxReturn($data,$seriesModel->getLastSql(),0);
			}
		}else{
			$this->ajaxReturn($_POST['type'],'设置商品系列失败2，请重试！',0);
		}
	}
}

//商品系列类
class series{	
	public $seriesStr=array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
}
?>